﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace RenameNum
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.Move;
            }
           
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                String[] files = e.Data.GetData(DataFormats.FileDrop, false) as String[];
                for(int i=0;i<files.Length;i++)
                {
                    listView1.Items.Add(i.ToString());
                    listView1.Items[i].SubItems.Add(files[i]);
                }

            }
            else
            {
                ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
                Point ptScreen = new Point(e.X, e.Y);
                Point pt = listView1.PointToClient(ptScreen);
                ListViewItem TargetItem = listView1.GetItemAt(pt.X, pt.Y);//拖动的项将放置于该项之前   
                if (null == TargetItem)
                    return;

                listView1.Items.Insert(TargetItem.Index, (ListViewItem)draggedItem.Clone());
                listView1.Items.Remove(draggedItem);
            }
            
        }
        void SetHanghao()
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                listView1.Items[i].Text = i.ToString();
            }
        }

        private void button_up_Click(object sender, EventArgs e)
        {

            moveUp();
        }
        void moveUp()
        {
            if (listView1.SelectedItems.Count > 0 && listView1.SelectedItems[0].Index != 0)
            {
                listView1.BeginUpdate();
                foreach (ListViewItem lvi in listView1.SelectedItems)
                {
                    ListViewItem item = lvi;
                    int index = lvi.Index;
                    listView1.Items.RemoveAt(index);
                    listView1.Items.Insert(index - 1, item);
                }
                SetHanghao();
                listView1.EndUpdate();
            }
            listView1.Focus(); 
        }
        void moveDown()
        {

            if (listView1.SelectedItems.Count > 0 && listView1.SelectedItems[listView1.SelectedItems.Count - 1].Index < (listView1.Items.Count - 1))
            {
                listView1.BeginUpdate();
                int count = listView1.SelectedItems.Count;
                foreach (ListViewItem lvi in listView1.SelectedItems)
                {
                    ListViewItem item = lvi;
                    int index = lvi.Index;
                    listView1.Items.RemoveAt(index);
                    listView1.Items.Insert(index + count, item);
                }
                SetHanghao();
                listView1.EndUpdate();
            }
            listView1.Focus(); 
        }
        private void button_down_Click(object sender, EventArgs e)
        {
            moveDown();
        }

        private void button_Rename_Click(object sender, EventArgs e)
        {
            for(int i=0;i<listView1.Items.Count;i++)
            {
               listView1.Items[i].SubItems[1].Text=  RenameFile(listView1.Items[i].SubItems[1].Text, i.ToString().PadLeft(3,'0'));
                
            }
        }
        string RenameFile(string filepath,string newname)
        {
            try
            {
                string path;string newfile="";
                if(File.Exists(filepath))//如果是文件
                {
                    path = Path.GetDirectoryName(filepath);
                    string extension = Path.GetExtension(filepath);
                     newfile=path + "\\" + newname+extension;
                    File.Move(filepath, newfile);
                    
                }
                else if(Directory.Exists(filepath))//如果是文件夹
                {
                    path = filepath;
                    string parentdir = path.Substring(0,path.LastIndexOf('\\'));
                    newfile = parentdir + "\\"+newname;
                    Directory.Move(filepath, newfile);
                }
                return newfile;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return "";
            }
           
        }

        private void menu_delete_Click(object sender, EventArgs e)
        {
            for(int i=0;i<listView1.SelectedItems.Count;i++)
            {
                listView1.Items.Remove(listView1.SelectedItems[i]);
            }
        }

        private void menu_clear_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }

        private void listView1_DragOver(object sender, DragEventArgs e)
        {
            Point ptScreen = new Point(e.X, e.Y);
            Point pt = listView1.PointToClient(ptScreen);
            ListViewItem item = listView1.GetItemAt(pt.X, pt.Y);
            if (item != null)
                item.Selected = true;
        }

        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listView1.DoDragDrop(e.Item, DragDropEffects.Move);
        }

        private void Menu_up_Click(object sender, EventArgs e)
        {
            moveUp();
        }

        private void Menu_down_Click(object sender, EventArgs e)
        {
            moveDown();
        }
    }
}
